#-*- coding:UTF-8 -*-
import json
import re
import time

import requests
from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.SongList.models import Song, SongList
from apps.payment.models import OrderInfo
from apps.users.models import PlayList_Follow
from utils.playlist_detail import playlist_detail
from utils.singer_detail import vip
from utils.song_detail import song_comment, song_detail, song_similar, song_mv


class Play_List(View):
    """歌单"""
    def get(self, request):

        # 获取华语歌单
        chinese_playlist = SongList.objects.filter(song_list_category=6)
        # 获取韩语歌单
        korean_playlist = SongList.objects.filter(song_list_category=7)
        # 获取日本歌单
        japan_playlist = SongList.objects.filter(song_list_category=8)
        # 获取流行歌单
        liuxing_playlist = SongList.objects.filter(song_list_category=9)
        # 获取说唱歌单
        talk_playlist = SongList.objects.filter(song_list_category=10)
        # 获取经典歌单
        classic_playlist = SongList.objects.filter(song_list_category=11)
        # 获取治愈歌单
        cure_playlist = SongList.objects.filter(song_list_category=12)
        # 获取运动歌单
        sports_playlist = SongList.objects.filter(song_list_category=13)
        # 获取VIP信息
        user_id = request.session.get('user_id')
        user_vip = vip(user_id)
        content = {
            'chinese_playlist': chinese_playlist,
            'korean_playlist': korean_playlist,
            'japan_playlist': japan_playlist,
            'liuxing_playlist': liuxing_playlist,
            'talk_playlist': talk_playlist,
            'classic_playlist': classic_playlist,
            'cure_playlist': cure_playlist,
            'sports_playlist': sports_playlist,
            'user_vip': user_vip,
            'user_id': user_id,
        }
        return render(request, 'playlist.html', content)


class Song_List(View):
    """歌单详情"""
    def get(self, request):
        song_id = request.GET.get('id')
        # 歌单详情, 所有歌曲， 所有歌曲的id
        playlist_details, songs_all, songs_id, count = playlist_detail(song_id)
        user_id = request.session.get('user_id')
        songlist = PlayList_Follow.objects.filter(users_id=user_id, playlist_id=song_id)
        is_song_list = None
        for x in songlist:
            is_song_list = x.playlist_id
        # 获取VIP信息
        user_id = request.session.get('user_id')
        user_vip = vip(user_id)
        is_vip = OrderInfo.objects.filter(user_id=user_id)

        context = {
            'song_list': songs_all,
            'list_dict': playlist_details,
            'all': songs_id,
            'playlist_id': song_id,
            'user_id': user_id,
            'count': count,
            'is_song_list': is_song_list,
            'user_vip': user_vip,
            'is_vip': is_vip
        }


        return render(request, 'song_list_detail.html', context)

    def post(self, request):
        json_dict = json.loads(request.body.decode())
        active_like = json_dict.get('active_like')
        playlist_id = json_dict.get('playlist_id')
        playlist_name = json_dict.get('playlist_name')
        playlist_img = json_dict.get('playlist_img')
        playlist_song_count = json_dict.get('playlist_song_count')
        playlist_play_count = json_dict.get('playlist_play_count')
        user_id = json_dict.get('user_id')

        if not all([active_like, playlist_name, playlist_id, playlist_img, playlist_play_count, playlist_song_count]):
            return http.JsonResponse({'errmsg': '参数错误', 'errno': 7})
        if not user_id:
            return http.JsonResponse({'errmsg': '登录后即可收藏', 'errno': 6})

        try:
            songlist = PlayList_Follow.objects.create(playlist_id=playlist_id,
                                    playlist_name=playlist_name,
                                    playlist_pic=playlist_img,
                                    playlist_song_count=playlist_song_count,
                                    playlist_play_count=playlist_play_count,
                                    users_id=user_id)
            songlist.save()
            return http.JsonResponse({'errmsg': '收藏成功', 'errno': 0})

        except Exception as e:
            print('=========', e)
            return http.JsonResponse({'errmsg': '收藏失败', 'errno': 4})

    def delete(self, request):
        # 删除关注
        json_dict = json.loads(request.body.decode())
        del_like = json_dict.get('del_like')
        playlist_id = json_dict.get('playlist_id')
        user_id = json_dict.get('user_id')

        if not all([del_like, playlist_id, user_id]):
            return http.JsonResponse({'errmsg': '参数错误', 'errno': 5})
        else:
            PlayList_Follow.objects.filter(users_id=user_id, playlist_id=playlist_id).delete()

            return http.JsonResponse({'errmsg': '取消收藏成功', 'errno': 0})


class Song_Detail(View):
    """歌曲详情"""
    def get(self, request):
        song_id = request.GET.get('id')
        # 用户评论
        user_comment = song_comment(song_id)
        # 歌曲详情
        song_details = song_detail(song_id)
        # 获取类似歌单
        similar_list = song_similar(song_id)
        # 获取相关mv
        mv_song = song_mv(song_id)
        print('00000000', mv_song[0]['mv_cover'])
        # 判断是否收藏此歌曲
        sing_id_url = 'http://musicapi.leanapp.cn/song/detail?ids=' + str(song_id)
        contents = requests.get(sing_id_url)
        ret = json.loads(contents.text)
        artists_ids = ret['songs'][0]['ar'][0]['id']

        is_song = None
        users_id = request.session.get('user_id')
        if users_id:
            songss = Song.objects.filter(user_id=users_id, song_id=song_id)
            for x in songss:
                is_song = x.is_song
        else:
            is_song = None

        playlist_name = PlayList_Follow.objects.filter(users_id=users_id)
        # 获取VIP信息
        user_id = request.session.get('user_id')
        user_vip = vip(user_id)
        is_vip = OrderInfo.objects.filter(user_id=user_id)

        context = {
            'user_lists': user_comment,
            'lists': song_details,
            'similar_lists': similar_list,
            'mv_song': mv_song,
            'song_id': song_id,
            'artists_ids': artists_ids,
            'is_song': is_song,
            'playlist_name': playlist_name,
            'users_id': users_id,
            'user_vip': user_vip,
            'user_id': user_id,
            'is_vip': is_vip
        }

        return render(request, 'song_detail.html', context)

    def post(self, request):
        json_dict = json.loads(request.body.decode())
        active_like = json_dict.get('active_like')
        song_id = json_dict.get('song_id')
        song_name = json_dict.get('song_name')
        singer_name = json_dict.get('singer_name')
        singer_id = json_dict.get('singer_id')
        album_name = json_dict.get('album_name')
        album_id = json_dict.get('album_id')
        user_id = json_dict.get('user_id')

        # 判断数据
        if album_id and active_like:
            if not user_id:
                return http.JsonResponse({'errmsg': '登录后即可收藏'})

            if not all([active_like, song_id]):
                return http.JsonResponse({'errmsg': '参数错误'})

            try:
                songs = Song.objects.filter(user_id=user_id, song_id=song_id)
                if songs:
                    return http.JsonResponse({'errmsg': '此歌曲以收藏', 'errno': 18})
            except:
                return http.JsonResponse({'errmsg': '登录后即可收藏', 'errno': 17})
            else:
                Song.objects.create(song_id=song_id,
                                    song_name=song_name,
                                    user_id=user_id,
                                    song_singer_id=singer_id,
                                    song_singer=singer_name,
                                    song_album_id=album_id,
                                    song_album= album_name,
                                    is_song=True,
                                    )
                return http.JsonResponse({'errmsg': '收藏成功', 'errno': 0})

        else:
            try:

                song_list_id = json_dict.get('song_list_id')
                song_name = json_dict.get('song_name')
                song_id = json_dict.get('song_id')
                playlist_singer_id = json_dict.get('singer_id')
                playlist_singer_name = json_dict.get('singer_name')
                playlist_song_album = json_dict.get('song_album')
                playlist_song_album_id = json_dict.get('song_album_id')
                playlist_user_id = json_dict.get('user_id')
                song_user = Song.objects.filter(song_list_id=song_list_id, song_id=song_id)
                if not playlist_user_id:
                    return http.JsonResponse({'errmsg': '登录后即可收藏', 'errno': 16})
                if song_user:
                    return http.JsonResponse({'errmsg': '此歌曲以被收藏', 'errno': 14})

                Song.objects.create(song_id=song_id,
                                    song_name=song_name,
                                    song_singer=playlist_singer_name,
                                    song_singer_id=playlist_singer_id,
                                    song_album=playlist_song_album,
                                    song_album_id=playlist_song_album_id,
                                    song_list_id=song_list_id,
                                    user_id=playlist_user_id
                                            )
                return http.JsonResponse({'errmsg': '添加成功', 'errno': 0})
            except Exception as e:
                print(e)
                return http.JsonResponse({'errmsg': '添加失败', 'errno': 9})


    def delete(self, request):
        """删除收藏"""
        # 获取数据
        json_dict = json.loads(request.body.decode())
        del_like = json_dict.get('del_like')
        song_id = json_dict.get('song_id')
        user_id = request.session.get('user_id')
        print(user_id, song_id, del_like)
        if not all([song_id, user_id]):
            return http.JsonResponse({'errmsg': '参数错误', 'errno': 1})

        else:
            Song.objects.filter(user_id=user_id, song_id=song_id).delete()

            return http.JsonResponse({'errmsg': '取消收藏成功', 'errno': 0})



